<!DOCTYPE html>
<html lang="${LANG}" class="dark">

<head>
    <meta charset="UTF-8">
    <title>QBT_TR(Add tags)QBT_TR[CONTEXT=TransferListWidget]</title>
    <link rel="stylesheet" href="css/style.css?v=${CACHEID}" type="text/css">
    <script defer src="scripts/localpreferences.js?v=${CACHEID}"></script>
    <script defer src="scripts/color-scheme.js?v=${CACHEID}"></script>
    <script defer src="scripts/misc.js?locale=${LANG}&v=${CACHEID}"></script>
    <script>
        "use strict";

        window.addEventListener("DOMContentLoaded", (event) => {
            window.addEventListener("keydown", (event) => {
                switch (event.key) {
                    case "Enter":
                        event.preventDefault();
                        document.getElementById("tagNameButton").click();
                        break;
                    case "Escape":
                        event.preventDefault();
                        window.parent.qBittorrent.Client.closeFrameWindow(window);
                        break;
                }
            });

            const searchParams = new URLSearchParams(window.location.search);
            const uriAction = window.qBittorrent.Misc.safeTrim(searchParams.get("action"));

            if (uriAction === "create")
                document.getElementById("legendText").textContent = "QBT_TR(Tag:)QBT_TR[CONTEXT=TagFilterWidget]";

            document.getElementById("tagName").focus();

            document.getElementById("tagNameButton").addEventListener("click", (e) => {
                e.preventDefault();
                e.stopPropagation();

                const tagName = document.getElementById("tagName").value.trim();

                const verifyTagName = (name) => {
                    if ((name === null) || (name === ""))
                        return false;
                    if (name.includes(",")) {
                        alert("QBT_TR(Invalid tag name)QBT_TR[CONTEXT=TagFilterWidget]");
                        return false;
                    }
                    return true;
                };

                switch (uriAction) {
                    case "set": {
                        const uriHashes = window.qBittorrent.Misc.safeTrim(searchParams.get("hashes"));
                        if ((uriHashes === null) || (uriHashes.length <= 0))
                            return;

                        fetch("api/v2/torrents/addTags", {
                                method: "POST",
                                body: new URLSearchParams({
                                    hashes: uriHashes,
                                    tags: tagName
                                })
                            })
                            .then((response) => {
                                if (!response.ok)
                                    return;

                                window.parent.qBittorrent.Client.closeFrameWindow(window);
                            });
                        break;
                    }

                    case "create":
                        if (!verifyTagName(tagName))
                            return;

                        fetch("api/v2/torrents/createTags", {
                                method: "POST",
                                body: new URLSearchParams({
                                    tags: tagName
                                })
                            })
                            .then((response) => {
                                if (!response.ok)
                                    return;

                                window.parent.qBittorrent.Client.closeFrameWindow(window);
                            });
                        break;
                }
            });
        });
    </script>
</head>

<body>
    <div style="padding: 10px 10px 0px 10px;">
        <label for="tagName" id="legendText" style="font-weight: bold;">QBT_TR(Comma-separated tags:)QBT_TR[CONTEXT=TransferListWidget]</label>
        <input type="text" id="tagName" style="width: 99%;">
        <div style="text-align: center; padding-top: 10px;">
            <input type="button" value="QBT_TR(Add)QBT_TR[CONTEXT=HttpServer]" id="tagNameButton">
        </div>
    </div>
</body>

</html>
